import requests,os
import re
import time
import json

class get_yq_info:

    def get_data_html(self):
            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1'}
            response = requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia?from=timeline&isappinstalled=0', headers=headers, timeout=3)
            # 请求页面

            response = str(response.content, 'utf-8')
            # 中文重新编码
            return response
            #返回了HTML数据

    def get_data_dictype(self):
            areas_type_dic_raw = re.findall('try { window.getAreaStat = (.*?)}catch\(e\)',self.get_data_html())
            areas_type_dic = json.loads(areas_type_dic_raw[0])
            return areas_type_dic
            #返回经过json转换过的字典化的数据

    def save_data_to_excle(self):
            self.make_dir()
            #调用方法检查数据目录是否存在，不存在则创建数据文件夹
            current_time = time.strftime("%Y-%m-%d", time.localtime())
            txt=open('D:/中国疫情数据/%s.txt'% current_time,'w',encoding='utf-8')
            txt.write('省份名称'+'\t'+'省份简称或城市名称'+'\t'+'确诊人数'+'\t'+'疑似人数'+'\t'+'治愈人数'+'\t'+'死亡人数'+'\t'+'地区编码')
            for province_data in self.get_data_dictype() :
                    provincename = province_data['provinceName']
                    provinceshortName = province_data['provinceShortName']
                    p_confirmedcount = province_data['confirmedCount']
                    p_suspectedcount = province_data['suspectedCount']
                    p_curedcount = province_data['curedCount']
                    p_deadcount = province_data['deadCount']
                    p_locationid = province_data['locationId']
                    # 用循环获取省级以及该省以下城市的数据
                    txt.write(provincename+'\t'+provinceshortName+'\t'+str(p_confirmedcount)+'\t'+str(p_suspectedcount)+'\t'
                              +str(p_curedcount)+'\t'+str(p_deadcount)+'\t'+str(p_locationid)+'\n')

                    # 在工作表里写入省级数据
                    for citiy_data in province_data['cities'] :
                            cityname = citiy_data['cityName']
                            c_confirmedcount = citiy_data['confirmedCount']
                            c_suspectedcount = citiy_data['suspectedCount']
                            c_curedcount = citiy_data['curedCount']
                            c_deadcount = citiy_data['deadCount']
                            c_locationid = citiy_data['locationId']
                            # 该部分获取某个省下某城市的数据
                            txt.write('\t'+cityname+'\t'+str(c_confirmedcount)+'\t'+str(c_suspectedcount)+'\t'+str(c_curedcount)
                                      +'\t'+str(c_deadcount)+'\t'+str(c_locationid)+'\n')
                            # 此处为写入行数累加，cities部分循环
            print('======数据爬取成功======')
            txt.close()

    def make_dir(self):
            file_path = 'D:/中国疫情数据/'
            if not os.path.exists(file_path):
                    os.makedirs(file_path)
                    print('======数据文件夹不存在=======')
                    print('======数据文件夹创建成功======')
                    print('======创建目录为%s======'%(file_path))
            else:
                    print('======数据保存在目录：%s======' % (file_path))
            #检查并创建数据目录

    def exe_task(self):
        get_yq_info().save_data_to_excle()
    #执行完整采集任务

get_yq_info().exe_task()
